#include "setlabels.hpp"
#include "ui_setlabels.h"

SetLabels::SetLabels(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::SetLabels)
{
    connect(this,SIGNAL(sendChartConf(Labels*)),parent,SLOT(getChartConf(Labels*)));
    connect(parent, SIGNAL(sendChartConf(Labels*)), this, SLOT(getChartConf(Labels*)));
    ui->setupUi(this);
    labels = new Labels();
}

SetLabels::~SetLabels()
{
    delete ui;
    delete labels;
}

void SetLabels::on_lineEdit_editingFinished() // Xlabel
{
    this->labels->_xLabel = ui->lineEdit->text().toAscii().data();
}

void SetLabels::on_lineEdit_2_editingFinished() //XMin
{
    this->labels->_xMin = ui->lineEdit_2->text().toAscii().data();
}

void SetLabels::on_lineEdit_3_editingFinished()
{
    this->labels->_xMax = ui->lineEdit_3->text().toAscii().data();
}

void SetLabels::on_lineEdit_4_editingFinished()
{
    this->labels->_yLabel = ui->lineEdit_4->text().toAscii().data();
}

void SetLabels::on_lineEdit_5_editingFinished()
{
    this->labels->_yMin = ui->lineEdit_5->text().toAscii().data();
}

void SetLabels::on_lineEdit_6_editingFinished()
{
    this->labels->_yMax = ui->lineEdit_6->text().toAscii().data();
}

void SetLabels::on_lineEdit_7_editingFinished()
{
    this->labels->_zLabel = ui->lineEdit_7->text().toAscii().data();
}

void SetLabels::on_lineEdit_8_editingFinished()
{
    this->labels->_zMin = ui->lineEdit_8->text().toAscii().data();
}

void SetLabels::on_lineEdit_9_editingFinished()
{
    this->labels->_zMax = ui->lineEdit_9->text().toAscii().data();
}

void SetLabels::on_buttonBox_accepted()
{
    emit this->sendChartConf(labels);
}

void SetLabels::on_lineEdit_13_editingFinished()
{
    this->labels->_Title = ui->lineEdit_13->text().toAscii().data();
}

void SetLabels::getChartConf(Labels *lab)
{
    this->labels = lab;
    ui->lineEdit_13->setText(QString::fromStdString(this->labels->_Title));
    ui->lineEdit->setText(QString::fromStdString(this->labels->_xLabel));
    ui->lineEdit_2->setText(QString::fromStdString(this->labels->_xMin));
    ui->lineEdit_3->setText(QString::fromStdString(this->labels->_xMax));
    ui->lineEdit_4->setText(QString::fromStdString(this->labels->_yLabel));
    ui->lineEdit_5->setText(QString::fromStdString(this->labels->_yMin));
    ui->lineEdit_6->setText(QString::fromStdString(this->labels->_yMax));
    ui->lineEdit_7->setText(QString::fromStdString(this->labels->_zLabel));
    ui->lineEdit_8->setText(QString::fromStdString(this->labels->_zMin));
    ui->lineEdit_9->setText(QString::fromStdString(this->labels->_zMax));

    for(unsigned int i=0; i<this->labels->_Labels.size(); ++i)
    {
        QString toAddLabel;
        toAddLabel = "(" + QString::fromStdString(this->labels->_Labels[i]._x) + ",";
        toAddLabel += QString::fromStdString(this->labels->_Labels[i]._y) + ")" + "\t";
        toAddLabel += QString::fromStdString(this->labels->_Labels[i]._text);
        QListWidgetItem *tempItem = new QListWidgetItem(toAddLabel);
        ui->listWidget->addItem(tempItem);
        toAddLabel.clear();
    }
}

void SetLabels::on_pushButton_clicked() //add label
{
    labels->addLabel(ui->lineEdit_11->text().toAscii().data(),
                     ui->lineEdit_12->text().toAscii().data(),
                     ui->lineEdit_10->text().toAscii().data());
    QString toAddLabel;
    toAddLabel = "(" + ui->lineEdit_11->text() + ",";
    toAddLabel += ui->lineEdit_12->text() + ")" + "\t";
    toAddLabel += ui->lineEdit_10->text();
    QListWidgetItem *tempItem = new QListWidgetItem(toAddLabel);
    ui->listWidget->addItem(tempItem);
}

void SetLabels::on_pushButton_2_clicked() //remove label
{
    if(this->labels->_Labels.size()>0){
        int nr = ui->listWidget->row(ui->listWidget->currentItem());
        if(this->labels->_Labels.size()>1)
            this->labels->_Labels.erase(this->labels->_Labels.begin()+nr);
        else
            this->labels->_Labels.pop_back();
        ui->listWidget->takeItem(nr);
    }
}
